import { ExtractJwt, Strategy } from 'passport-jwt';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException, Logger } from '@nestjs/common';
import { sercetkey } from './auth.module';
import { UserRegisterModel } from '../../user/model/user.dto';

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {

  constructor() {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      secretOrKey: sercetkey,
    });
  }

  async validate(payload: UserRegisterModel) {
    const testName = 'shan';
    // Logger.log(payload, 'JwtStrategy-validate');
    const { username } = payload;
    if (testName !== username) {
      throw new UnauthorizedException('没有找到用户');
    }
    return { userId: payload.password, username: payload.username };
  }
}
